<?php if(!defined('BASEPATH'))exit('No direct script access allowed');
class lienhe extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library('addin');
	}
	public function index()
	{
        $this->load->model('sanpham_model');
		$data1['noibat'] = $this->sanpham_model->getNoiBat();
		$this->form_validation->set_rules('hoten','','trim|required|min_length[2]|xss_clean');
		$this->form_validation->set_rules('phone','','trim|required|xss_clean');
		$this->form_validation->set_rules('email','','trim|required|valid_email|xss_clean');
		$this->form_validation->set_rules('noidung','','trim|required|min_length[20]|xss_clean');
		$this->form_validation->set_rules('captcha','','callback_captcha_check');
		$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
		if ($this->form_validation->run() == FALSE)
		{
			$expiration = time() - 300;
			$this->db->query("DELETE FROM lp_captcha WHERE captcha_time < ".$expiration);
			$vals = array (
				'img_path' => './upload/',
				'img_url' => base_url().'upload/',
				'font_path' => './locphucore/fonts/texb.ttf',
				'img_width' => '224',
				'img_height' => '35',
				'expiration' => '3600');
			$this->load->library('captcha');
			$cap = $this->captcha->create_captcha($vals);
			$data = array(
				'captcha_id' => '',
				'captcha_time' => $cap['time'],
				'ip_address' => $this->input->ip_address(),
				'word' => $cap['word']);
			$query = $this->db->insert_string('lp_captcha',$data);
			$this->db->query($query);
			$dataimage['image'] = $cap['image'];
			$data1['thongke'] = $this->addin->thongKe();
			$data1['counter'] = $this->addin->getcounter();
			$data1['online'] = $this->addin->getonline();
			$data1['menu1'] = $this->addin->getMenuMain();
			$data1['menu2'] = $this->addin->getMenu3Cap();
			$this->load->view('required/header-trangcon',$data1);
			$this->load->view('lienhe/index', $dataimage);
			$this->load->view('required/footer',TRUE);
		}
		else
		{

			$this->load->model('lienhe_model');
			$hoten = $this->input->post('hoten',TRUE);
			$phone = $this->input->post('phone',TRUE);
			$email = $this->input->post('email',TRUE);
			$noidung = $this->input->post('noidung',TRUE);
			if($this->lienhe_model->insert($hoten, $phone, $email, $noidung))
				echo redirect(base_url());
			else echo redirect(base_url('lienhe.html'));
		}
		//image : string (hinh captcha)
		//counter : int : So nguoi da truy cap
		//online : int : So nguoi dang online
		//$menu1 : string (Chỉnh sửa lại chuỗi xuất ra)
		//$menu2 : string (Chỉnh sửa lại chuỗi xuất ra)
	}
	function captcha_check($captcha)
	{
		$exp = time() - 600;
		$sql = "SELECT COUNT(*) AS count FROM lp_captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
		$binds = array ($this->input->post('captcha'), $this->input->ip_address(), $exp);
		$query = $this->db->query($sql, $binds);
		$row = $query->row();
		if ($row->count == 0)
		{
			$this->form_validation->set_message('captcha_check','*');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
}